//
//  CCDiscoverViewController.m
//  CwinCar
//
//  Created by dlios on 15/10/15.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "CCDiscoverViewController.h"
#import "LowPriceViewController.h"
#import "DownLoadViewController.h"
#import "CCDiscoverModel.h"
#import "LowPriceCollectionCell.h"
#import "CCDiscoverTableViewCell.h"
#import "LowPriceGoodsModel.h"
#import "RadioViewController.h"
#import "ActivityandPreViewController.h"
#import "BreakViewController.h"
#import "ConditionViewController.h"
@interface CCDiscoverViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
{
    BOOL tend;
}

@property (nonatomic, retain) NSMutableDictionary *getCollectionViewDic;
@property (nonatomic, retain) UITableView *LowPriceTableView;
@property (nonatomic, retain) NSMutableArray *pushArray;
@property (nonatomic, retain) UIImageView *headerImg;

@end

@implementation CCDiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"发现";
    [self creatTableView];
    [self creatCollectionViewData];
}

#pragma mark 请求collectionView数据
- (void)creatCollectionViewData
{
    self.getCollectionViewDic = [[NSMutableDictionary alloc]init];
    self.pushArray = [[NSMutableArray alloc]init];
    
    [AFNblock getdataWithString:@"http://jjfz01.qianfan365.com/jjfz/front/product/search.do?clientId=a860db5790414e4a9cbc01ed54eefecf" block:^(id block)
     {
         self.getCollectionViewDic = block;
         for (NSMutableDictionary *pushDic in [self.getCollectionViewDic objectForKey:@"custemcats"])
         {
             CCDiscoverModel *pushModel = [[CCDiscoverModel alloc]initWithDictionary:pushDic];
             [self.pushArray addObject:pushModel];
         }
         [self.LowPriceTableView reloadData];
     }];
    
}

#pragma mark 根据偏移量设置headerImg位置(呈放大缩小状)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y < - mainScree_Height / 3.705)
    {
        [self.headerImg setFrame:CGRectMake(0, scrollView.contentOffset.y, mainScreen_Width, - scrollView.contentOffset.y)];
    }
}

#pragma mark 建立选项tableView
- (void)creatTableView
{
    self.headerImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, - mainScree_Height / 3.705, mainScreen_Width, mainScree_Height / 3.705)];
    [self.headerImg setImage:[UIImage imageNamed:[NSString stringWithFormat:@"faxian.jpg"]]];
    self.headerImg.contentMode = UIViewContentModeScaleAspectFill;
    
    self.LowPriceTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width, mainScree_Height - 64) style:UITableViewStyleGrouped];
    self.LowPriceTableView.dataSource = self;
    self.LowPriceTableView.delegate = self;
    self.LowPriceTableView.showsVerticalScrollIndicator = NO;
    [self.LowPriceTableView setContentInset:UIEdgeInsetsMake(mainScreen_Width / 2.083, 0, 0, 0)];
    [self.LowPriceTableView addSubview:self.headerImg];
    [self.view addSubview:self.LowPriceTableView];
    //装文字的数组
    self.firstArray = [NSMutableArray arrayWithObject:@"汽车电台"];
    self.secondArray = [NSMutableArray arrayWithObject:@"优惠/活动"];
    self.thirdArray = [NSMutableArray arrayWithObjects:@"违章查询",@"条件找车",@"养车特惠", nil];
}

#pragma mark tableView点击跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0){
        RadioViewController *radio = [[RadioViewController alloc]init];
        radio.hidesBottomBarWhenPushed =YES;
        [self.navigationController pushViewController:radio animated:YES];
        
    }
    
    
    else if (indexPath.section == 1 && indexPath.row == 0)
    {
        ActivityandPreViewController *ABview = [[ActivityandPreViewController alloc]init];
        [self.navigationController pushViewController:ABview animated:YES];
        
    }
    else if (indexPath.section == 2 && indexPath.row == 0)
    {
        BreakViewController *webViewAll = [[BreakViewController alloc]init];
        webViewAll.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:webViewAll animated:YES];
    }
    else if (indexPath.section == 2 && indexPath.row == 1)
    {
        ConditionViewController *condition = [[ConditionViewController alloc]init];
        condition.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:condition animated:YES];
    }else if (indexPath.section == 2 && indexPath.row == 2){
        CCFormWebViewController *web = [[CCFormWebViewController alloc]init];
        web.hidesBottomBarWhenPushed = YES;
        web.webStr = @"http://app.autohome.com.cn/app/Ashx/RD/rd.ashx?id=1283&rd=http%3a%2f%2fyc.m.autohome.com.cn%2findex.html";
        [self.navigationController pushViewController:web animated:YES];
    }
    
}

#pragma mark tableView协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }else if (section == 1)
    {
        return 1;
    }else if (section == 2)
    {
        return 3;
    }else if (section == 3)
    {
        return 1;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 3)
    {
        return mainScree_Height / 9.528;
    }else
    {
        return mainScree_Height / 16.675;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 3)
    {
        return @"汽车内饰";
    }else
    {
        return @" ";
    }
}

#pragma mark 设置tableView头部和底部
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return mainScree_Height / 24.703;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    CCDiscoverArrayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell)
    {
        cell = [[CCDiscoverArrayTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
        if (indexPath.section == 0 && indexPath.row == 0)
        {
            cell.lable.text = self.firstArray[indexPath.row];
            [cell.imgeView setImage:[UIImage imageNamed:@"discoveryfunction_13"]];
            
            
        }else if (indexPath.section == 1 && indexPath.row == 0)
        {
            
            cell.lable.text = self.secondArray[indexPath.row];
            [cell.imgeView setImage:[UIImage imageNamed:@"discoveryfunction_14"]];
            
        }else if (indexPath.section == 2 )
        {
            cell.lable.text = self.thirdArray[indexPath.row];
            switch (indexPath.row) {
                case 0:
                    [cell.imgeView setImage:[UIImage imageNamed:@"discoveryfunction_8"]];
                    break;
                case 1:
                    cell.imgeView.image = [UIImage imageNamed:@"discoveryfunction_11"];
                    break;
                case 2:
                    cell.imgeView.image = [UIImage imageNamed:@"discoveryfunction_10"];
                    break;
                default:
                    break;
            }
        }
    }
    static NSString *cellId2 = @"cell2";
    CCDiscoverTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:cellId2];
    if (!cell2)
    {
        cell2 = [[CCDiscoverTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId2];
    }
    cell2.dataSource = self;
    cell2.delegate = self;
    
    if (indexPath.section == 3 && indexPath.row == 0)
    {
        return cell2;
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

#pragma mark collectionView协议方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.pushArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LowPriceCollectionCell *cell3 = [collectionView dequeueReusableCellWithReuseIdentifier:@"LjjLowPriceCollectionViewCell" forIndexPath:indexPath];
    CCDiscoverModel *modelPS = [self.pushArray objectAtIndex:indexPath.item];
    [cell3.imgView sd_setImageWithURL:[NSURL URLWithString:modelPS.catpicpath]placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
    
    return cell3;
}

#pragma mark collectionView点击跳转并传值
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DownLoadViewController *downLoadView = [[DownLoadViewController alloc]init];
    downLoadView.hidesBottomBarWhenPushed =YES;
    [self.navigationController pushViewController:downLoadView animated:YES];
    CCDiscoverModel *geTModel = [[CCDiscoverModel alloc]init];
    
    geTModel = self.pushArray[indexPath.item];
    
    downLoadView.getDownModel = geTModel;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
